<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="support.*, java.util.*" %>
    
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Citizenship</title>
<link href="main.css" rel="stylesheet" type="text/css">

</head>
<body>
<%
	// Get name from HTTP request
	String first_name = request.getParameter("first_name");
	String last_name = request.getParameter("last_name");
	String middle_name = request.getParameter("middle_name");
	
	// Store name in session with map
	Map<String, String> name = new HashMap<String, String>();
	if (first_name != "" && last_name != ""){
		name.put("first_name", first_name);
		name.put("last_name", last_name);
		name.put("middle_name", middle_name);
		session.setAttribute("name", name);
	}
	
	// Get countries from text file
	support s = new support();
	Vector countries = (Vector)application.getAttribute("countries");
	if(countries == null)
	{
		String path = config.getServletContext().getRealPath("countries.txt");
		countries = s.getCountries(path);
		application.setAttribute("countries",countries);
	}

%>

<h1>Your information</h1>
Name: <%= name.get("first_name") + " " + name.get("last_name") + " " + name.get("middle_name") %>
<hr>

<h1>Please choose your citizenship</h1>
<table class="multicolumn">
	<tr>
		<% 
		int cols = 3;
		int itemsPerCol = countries.size() / cols;
		if (countries.size() % cols != 0) itemsPerCol += 1;
		for(int i=0; i<countries.size(); i++){
			if (i % itemsPerCol == 0)  out.println("<td style=\"vertical-align:top\">");
			out.println("<a href=\"residence.jsp?citizenshipID=" + i + "&citizenship=" + countries.get(i) + "\" >" + countries.get(i) + "</a><br />");
			if (i % itemsPerCol == itemsPerCol-1 || i==countries.size() -1 )  out.println("</td>");
		};
		%>
	</tr>
</table>
	
</body>
</html>